<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>python基础之列表、字符串、字典、集合 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>python基础之列表、字符串、字典、集合</h1>
  <time datetime=2020-06-26T12:08:28&#43;0800 class="post-date">Fri, Jun 26, 2020</time>
  <h2 id="列表">列表</h2>
<p><strong>1. append用于在列表末尾追加新的对象</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">*</span> a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]
 a<span style="color:#f92672">.</span>append(<span style="color:#ae81ff">4</span>)             <span style="color:#75715e">#the result： [1, 2, 3, 4]</span>
</code></pre></div><p><strong>2、count方法统计某个元素在列表中出现的次数</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> a <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;aa&#39;</span>,<span style="color:#e6db74">&#39;bb&#39;</span>,<span style="color:#e6db74">&#39;cc&#39;</span>,<span style="color:#e6db74">&#39;aa&#39;</span>,<span style="color:#e6db74">&#39;aa&#39;</span>]
 <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>count(<span style="color:#e6db74">&#39;aa&#39;</span>))         <span style="color:#75715e">#the result ： 3</span>
</code></pre></div><p><strong>3. extend方法可以在列表的末尾一次性追加另一个序列中的多个值</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]
 b <span style="color:#f92672">=</span> [<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>]
 a<span style="color:#f92672">.</span>extend(b)             <span style="color:#75715e">#the result ：[1, 2, 3, 4, 5, 6]</span>
</code></pre></div><p><strong>4. index函数用于从列表中找出某个值第一个匹配项的索引位置</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span>]
 <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>index(<span style="color:#ae81ff">1</span>))          <span style="color:#75715e">#the result ： 0</span>
</code></pre></div><p><strong>5. insert方法用于将对象插入到列表中</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]
 a<span style="color:#f92672">.</span>insert(<span style="color:#ae81ff">0</span>,<span style="color:#e6db74">&#39;aa&#39;</span>)      <span style="color:#75715e">#the result : [&#39;aa&#39;, 1, 2, 3]</span>
</code></pre></div><p><strong>6. pop</strong>方法会移除列表中的一个元素（默认是最后一个），并且返回该元素的值**</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]
a<span style="color:#f92672">.</span>pop()               					
a<span style="color:#f92672">.</span>pop(<span style="color:#ae81ff">0</span>)

<span style="color:#75715e">#the result ： [1, 2]</span>
</code></pre></div><p><strong>7. remove</strong>方法用于移除列表中某个值的第一个匹配项**</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;aa&#39;</span>,<span style="color:#e6db74">&#39;bb&#39;</span>,<span style="color:#e6db74">&#39;cc&#39;</span>,<span style="color:#e6db74">&#39;aa&#39;</span>]
a<span style="color:#f92672">.</span>remove(<span style="color:#e6db74">&#39;aa&#39;</span>)           

<span style="color:#75715e">#the result ： [&#39;bb&#39;, &#39;cc&#39;, &#39;aa&#39;]</span>
</code></pre></div><p><strong>8. reverse方法将列表中的元素反向存放</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>,<span style="color:#e6db74">&#39;c&#39;</span>]
a<span style="color:#f92672">.</span>reverse()             

<span style="color:#75715e">#the result *： [&#39;c&#39;, &#39;b&#39;, &#39;a&#39;]</span>
</code></pre></div><p><strong>9. sort方法用于在原位置对列表进行排序，意味着改变原来的列表，让其中的元素按一定顺序排列</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> a <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;a&#39;</span>,<span style="color:#e6db74">&#39;b&#39;</span>,<span style="color:#e6db74">&#39;c&#39;</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]
 a<span style="color:#f92672">.</span>sort()              

<span style="color:#75715e">#the result ：[1, 2, 3, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</span>
</code></pre></div><p><strong>10. enumrate</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> li <span style="color:#f92672">=</span> [<span style="color:#ae81ff">11</span>,<span style="color:#ae81ff">22</span>,<span style="color:#ae81ff">33</span>]
 <span style="color:#66d9ef">for</span> k,v <span style="color:#f92672">in</span> enumerate(li, <span style="color:#ae81ff">1</span>):
   <span style="color:#66d9ef">print</span>(k,v)
</code></pre></div><h2 id="字符串">字符串</h2>
<p><strong>1. find方法可以在一个较长的字符串中查找子串，他返回子串所在位置的最左端索引，如果没有找到则返回-1</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;abcdefghijk&#39;</span>
 <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>find(<span style="color:#e6db74">&#39;abc&#39;</span>))                 		<span style="color:#75715e">#the result ： 0</span>
<span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>find(<span style="color:#e6db74">&#39;abc&#39;</span>,<span style="color:#ae81ff">10</span>,<span style="color:#ae81ff">100</span>))              	<span style="color:#75715e">#the result ： 11 指定查找的起始和结束查找位置</span>
</code></pre></div><p><strong>2. join方法是非常重要的字符串方法，他是split方法的逆方法，用来连接序列中的元素，并且需要被连接的元素都必须是字符串</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;1&#39;</span>,<span style="color:#e6db74">&#39;2&#39;</span>,<span style="color:#e6db74">&#39;3&#39;</span>]
<span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;+&#39;</span><span style="color:#f92672">.</span>join(a))                                    

<span style="color:#75715e">#the result ： 1+2+3</span>
</code></pre></div><p><strong>3. split方法,是一个非常重要的字符串，它是join的逆方法，用来将字符串分割成序列</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#39;1+2+3+4&#39;</span><span style="color:#f92672">.</span>split(<span style="color:#e6db74">&#39;+&#39;</span>))                          

<span style="color:#75715e">#the result ： [&#39;1&#39;, &#39;2&#39;, &#39;3&#39;, &#39;4&#39;]</span>
</code></pre></div><p><strong>4. strip 方法返回去除首位空格（不包括内部）的字符串</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;   test   test    &#34;</span><span style="color:#f92672">.</span>strip())                  

<span style="color:#75715e">#the result ：“test   test”</span>
</code></pre></div><p><strong>5. replace方法返回某字符串所有匹配项均被替换之后得到字符串</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(<span style="color:#e6db74">&#34;This is a test&#34;</span><span style="color:#f92672">.</span>replace(<span style="color:#e6db74">&#39;is&#39;</span>,<span style="color:#e6db74">&#39;is_test&#39;</span>))     

<span style="color:#75715e">#the result ： This_test is_test a test</span>
</code></pre></div><h2 id="字典">字典</h2>
<p><strong>1. clear方法清除字典中所有的项，这是一个原地操作，所以无返回值（或则说返回None）</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;name&#39;</span>:<span style="color:#e6db74">&#34;tom&#34;</span>}
 d<span style="color:#f92672">.</span>clear()
 <span style="color:#66d9ef">print</span>(d)                     <span style="color:#75715e">#the result : {}</span>
</code></pre></div><p><strong>2. fromkeys方法使用给定的键建立新的字典，每个键都对应一个默认的值None</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> <span style="color:#66d9ef">print</span>({}<span style="color:#f92672">.</span>fromkeys([<span style="color:#e6db74">&#39;name&#39;</span>,<span style="color:#e6db74">&#39;age&#39;</span>]))       
 
 <span style="color:#75715e">#the result　：　{&#39;age&#39;: None, &#39;name&#39;: None}</span>
</code></pre></div><p><strong>3. get方法是个更宽松的访问字典项的方法，如果试图访问字典中不存在的项时不会报错仅会   返回：None</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">8777</span>,<span style="color:#e6db74">&#39;Jack&#39;</span>:<span style="color:#ae81ff">8888</span>,<span style="color:#e6db74">&#39;Fly&#39;</span>:<span style="color:#ae81ff">6666</span>}
<span style="color:#66d9ef">print</span>(d<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;Tom&#39;</span>))               		<span style="color:#75715e">#the result ：   8777</span>
<span style="color:#66d9ef">print</span>(d<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;not_exist&#39;</span>))            	<span style="color:#75715e">#the result ：   None</span>
</code></pre></div><p><strong>4. for循环字典的三种方法</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">8777</span>,<span style="color:#e6db74">&#39;Jack&#39;</span>:<span style="color:#ae81ff">8888</span>,<span style="color:#e6db74">&#39;Fly&#39;</span>:<span style="color:#ae81ff">6666</span>}
 <span style="color:#66d9ef">for</span> k,v <span style="color:#f92672">in</span> d<span style="color:#f92672">.</span>items():
   <span style="color:#66d9ef">print</span>(k,v)
 <span style="color:#66d9ef">for</span> k <span style="color:#f92672">in</span> d<span style="color:#f92672">.</span>values():
   <span style="color:#66d9ef">print</span>(k)
 <span style="color:#66d9ef">for</span> k <span style="color:#f92672">in</span> d<span style="color:#f92672">.</span>keys():
   <span style="color:#66d9ef">print</span>(k)
</code></pre></div><p><strong>5. pop方法用于获得对应与给定键的值，然后将这个”键-值”对从字典中移除</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"> d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">8777</span>,<span style="color:#e6db74">&#39;Jack&#39;</span>:<span style="color:#ae81ff">8888</span>,<span style="color:#e6db74">&#39;Fly&#39;</span>:<span style="color:#ae81ff">6666</span>}
 v <span style="color:#f92672">=</span> d<span style="color:#f92672">.</span>pop(<span style="color:#e6db74">&#39;Tom&#39;</span>)
 <span style="color:#66d9ef">print</span>(v)          		<span style="color:#75715e">#8777</span>
</code></pre></div><p><strong>6. setdefault方法在某种程度上类似于get方法，能够获得与给定键相关联的值，除此之外，setdefault还能在字典中不含有给定键的情况下设定相应的键值</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">8777</span>,<span style="color:#e6db74">&#39;Jack&#39;</span>:<span style="color:#ae81ff">8888</span>,<span style="color:#e6db74">&#39;Fly&#39;</span>:<span style="color:#ae81ff">6666</span>}
d<span style="color:#f92672">.</span>setdefault(<span style="color:#e6db74">&#39;Tom&#39;</span>)              		<span style="color:#75715e">#the result ： 8777</span>
<span style="color:#66d9ef">print</span>(d<span style="color:#f92672">.</span>setdefault(<span style="color:#e6db74">&#39;Test&#39;</span>))         	<span style="color:#75715e">#the result ： None</span>
<span style="color:#66d9ef">print</span>(d)                <span style="color:#75715e">#{&#39;Fly&#39;: 6666, &#39;Jack&#39;: 8888, &#39;Tom&#39;: 8777, &#39;Test&#39;: None}</span>
</code></pre></div><p><strong>7. update方法可以利用一个字典项更新另一个字典，提供的字典中的项会被添加到旧的字典中，如有相同的键则会被覆盖</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">d <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">8777</span>,<span style="color:#e6db74">&#39;Jack&#39;</span>:<span style="color:#ae81ff">8888</span>,<span style="color:#e6db74">&#39;Fly&#39;</span>:<span style="color:#ae81ff">6666</span>}
a <span style="color:#f92672">=</span> {<span style="color:#e6db74">&#39;Tom&#39;</span>:<span style="color:#ae81ff">110</span>,<span style="color:#e6db74">&#39;Test&#39;</span>:<span style="color:#ae81ff">119</span>}
d<span style="color:#f92672">.</span>update(a)
<span style="color:#66d9ef">print</span>(d)             <span style="color:#75715e">#the result ：{&#39;Fly&#39;: 6666, &#39;Test&#39;: 119, &#39;Jack&#39;: 8888, &#39;Tom&#39;: 110}</span>
</code></pre></div><p><strong>8. 将两个列表组合成字典</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">keys <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;a&#39;</span>, <span style="color:#e6db74">&#39;b&#39;</span>]
values <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>]
<span style="color:#66d9ef">print</span>(dict(zip(keys,values)))                   <span style="color:#75715e"># {&#39;a&#39;: 1, &#39;b&#39;: 2}</span>
</code></pre></div><h2 id="集合---list_1--1234512">集合   list_1 = [1,2,3,4,5,1,2]</h2>
<p><strong>1、去重(去除list_1中重复元素1,2)</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">list_1 <span style="color:#f92672">=</span> set(list_1)                                    <span style="color:#75715e">#去重： {1, 2, 3, 4, 5}</span>
<span style="color:#66d9ef">print</span>(list_1)
list_2 <span style="color:#f92672">=</span> set([<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">8</span>])
</code></pre></div><p><strong>2、交集（在list_1和list_2中都有的元素4,5）</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(list_1<span style="color:#f92672">.</span>intersection(list_2))                      <span style="color:#75715e">#交集： {4, 5}</span>
</code></pre></div><p><strong>3、并集（在list_1和list_2中的元素全部打印出来，重复元素仅打印一次）</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(list_1<span style="color:#f92672">.</span>union(list_2))                             

<span style="color:#75715e">#并集： {1, 2, 3, 4, 5, 6, 7, 8}</span>
</code></pre></div><p><strong>4、差集</strong></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#66d9ef">print</span>(list_1<span style="color:#f92672">.</span>difference(list_2))                        
<span style="color:#75715e">#差集：在list_1中有在list_2中没有：   {1, 2, 3}</span>
<span style="color:#66d9ef">print</span>(list_2<span style="color:#f92672">.</span>difference(list_1))                        
<span style="color:#75715e">#差集：在list_1中有在list_2中没有：   {8, 6, 7}</span>

</code></pre></div>
</div>


    </main>

    
  </body>
</html>
